<?php
//模块名_控制名
class User_MenuController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "user/menu/";	//控制对应模板目录
	}
	
	public function indexAction(){				
//		$this->view->str = 'my test.';
		$cond=array();
		$total = User_Service_Menu::getByCond(array(),'count(*)','um_id',0,0);		
		$this->view->total = $total;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
    public function listAction(){
    	$um_title = $this->_request->getParam('um_title','');
    	$cond =array();
    	if(!empty($um_title)){
    		$cond['um_title'] = $um_title;
    	}

    	$page = $this->_request->getParam('page',1);
    	$pageSize = $this->_request->getParam('pageSize');
		$page = $page?$page:1;
    	$orderby = 'um_id';
		$result = User_Service_Menu::getByCond($cond,'*',$orderby, $pageSize, ($page - 1)*$pageSize);
		
		$total = User_Service_Menu::getByCond($cond,'count(*)',$orderby, 0, 0);
		$data=array('ask'=>0);
		if($total>0){
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
    }
    
    public function editAction(){
    	$um_id = $this->_request->getParam('um_id','');
    	$um_title = $this->_request->getParam('um_title','');
    	$um_en_title = $this->_request->getParam('um_en_title','');
    	$um_url = $this->_request->getParam('um_url','');
    	$um_css = $this->_request->getParam('um_css','');
    	$um_sort = $this->_request->getParam('um_sort','');
    	
    	$row = array(
			'um_title' => $um_title,
			'um_en_title' => $um_en_title,
			'um_url' => $um_url,
			'um_css' => $um_css,
			'um_sort' => $um_sort,
		);
		
		if($um_id){
			$result = User_Service_Menu::update($row,$um_id);
		}else {
			$result = User_Service_Menu::add($row);
		}
		
		if($result){
			$msg = array('ask'=>1);
    	}else{
    		$msg = array('ask'=>0);
    	}
		echo json_encode($msg);	
    }
    
    public function deleteAction(){
    	$um_id = $this->_request->getParam('id','');
    	if(!empty($um_id)){
    		$result = User_Service_Menu::delete($um_id);
    		if($result){
    			$msg = array('ask'=>1);
    		}else{
    			$msg = array('ask'=>0);
    		}
    		echo json_encode($msg);
    	}

    }
    
    public function singleAction(){
    	$um_id = $this->_request->getParam('id','');
    	if($um_id){
    		$result = User_Service_Menu::getByUmid($um_id);
    		$data=array('ask'=>0);
    		if( count($result) ){
				$data=array(
					'result'=>$result,
					'ask'=>1
				);				
			}
			die(Zend_Json::encode($data));
    	}
    }
    
}